package com.richy.toweather.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.richy.toweather.R;
import com.richy.toweather.adapter.WeatherRecyclerAdapter;
import com.richy.toweather.contract.MainContract;
import com.richy.toweather.entity.WeatherEntity;
import com.richy.toweather.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements MainContract.View {

    private ProgressBar mProgressBar;
    private RecyclerView mRecyclerViewMain;
    private WeatherRecyclerAdapter mWeatherRecyclerAdapter;
    private List<WeatherEntity> mWeatherEntityList = new ArrayList<>();

    private MainContract.Presenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPresenter = new MainPresenter(MainActivity.this);
        String[] cities = new String[]{"杭州", "绍兴", "成都", "武汉", "重庆", "南京", "天津", "苏州", "西安", "长沙", "沈阳",
                "青岛", "郑州", "大连", "宁波", "厦门", "福州", "无锡", "合肥", "昆明", "哈尔滨", "济南", "佛山", "长春", "温州"};
        initView();
        mPresenter.getDatas(cities);
    }

    private void initView() {
        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
        mRecyclerViewMain = (RecyclerView) findViewById(R.id.recyclerview_main);
        mWeatherRecyclerAdapter = new WeatherRecyclerAdapter(getApplicationContext());
        mRecyclerViewMain.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        mRecyclerViewMain.setAdapter(mWeatherRecyclerAdapter);
    }

    @Override
    public void showLoading(boolean loading) {
        if (loading) {
            mProgressBar.setVisibility(View.VISIBLE);
        } else {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void setData(List<WeatherEntity> weatherEntities) {
        mWeatherRecyclerAdapter.setWeatherEntityList(weatherEntities);
    }

    @Override
    public void dataError(Throwable e) {
        Toast.makeText(this, "加载失败...", Toast.LENGTH_LONG).show();
    }
}
